<?php
/**
 * 
 * 公共工具类
 * @author ChunYang.Jiang<chunyang@staff.sina.com.cn>
 * @copyright sina
 * @date 2011-04-09
 *
 */
class Util
{
	/**
	 * 
	 * 没有什么用，防止当前类被实例化
	 */
	private function __construct(){}
	/**
	 * 
	 * 字符串过滤
	 * @param unknown_type $str
	 */
	public static function filterStr($str)
	{
		
	}
	
	/**
	 * 
	 * 返回模板引擎
	 */
	public static function getTpl()
	{
		$tpl = new Smarty();
		$tpl->template_dir = ROOT_DIR . 'Templates/';
		$tpl->cache_dir = TMP_PATH;
		$tpl->config_dir = TMP_PATH;
		$tpl->compile_dir = TMP_PATH;
		$tpl->left_delimiter = '<{';
		$tpl->right_delimiter = '}>';
		return $tpl;
	}
	
	/**
	 * 
	 * 取得hash值
	 * @param hash $data 待hash变量
	 * @param unknown_type hash基数
	 */
	public static function hashData($data, $num)
	{
		$hash = sprintf('%u', crc32($data));
		$hash = intval($hash / $num);
		$hash = intval($hash % $num);
		$hash = dechex($hash);
		return sprintf('%02s', $hash);
	}
	
	/**
	 * 设置cookies
	 * @param string $key
	 * @param unknow $value
	 */
	public static function setCookies($key, $value)
	{
		$value = serialize($value);
		setcookie($key, $value, time() + COOKIE_EXPIRE, '/');
	}
	
	/**
	 * 返回cookies
	 * @param string $key
	 */
	public static function getCookies($key)
	{
		if (isset($_COOKIE[$key])){
			return unserialize($_COOKIE[$key]);
		}
		return false;
	}
	
	/**
	 * 清空cookies
	 * @param string $key
	 */
	public static function clearCookies($key)
	{
		setcookie($key, '', time()-1, '/');
	}
	
	/**
	 * 页面跳转
	 * @param 目标 $url
	 */
	public static function redirect($url = 'http://weibo.com') {
		@header ( "HTTP/1.1 302 Moved Temporarily" );
		@header ( "Status: 302 Moved Temporarily" );
		@header ( "Location: " . $url );
		exit ();
	}
}